package control;

import java.io.File;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.member.MemberService;
import model.member.MemberVO;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class MemberController extends MultiActionController {
	private MemberService service;
	private String path;
	public MemberController() {
		super();
		// TODO Auto-generated constructor stub
	}

	public MemberController(MemberService service, String path) {
		super();
		this.service = service;
		this.path = path;
	}
	public ModelAndView view(HttpServletRequest request, HttpServletResponse response){
		String page = request.getParameter("page");
		String viewName = "home";
		if(!page.equals("home")){
			viewName = page+".member";
		}
		return new ModelAndView(viewName);
	}
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response){
		String id = request.getParameter("id");
		String password = request.getParameter("password");
		MemberVO vo = null;
		HttpSession session = request.getSession();
		try {
			vo = service.login(id, password);
			session.setAttribute("mvo", vo);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return new ModelAndView("JsonView","mvo",vo);
	}
	public ModelAndView logout(HttpServletRequest request, HttpServletResponse response, HttpSession session){
		if(session!=null)
			session.invalidate();
		return new ModelAndView("home");
	}
	public ModelAndView register(HttpServletRequest request, HttpServletResponse response, MemberVO vo){
		try {
			// 프로필 이미지가 저장될 경로를 같이 넘겨준다
			service.register(vo, path);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("main.member");
	}
	public ModelAndView idCheck(HttpServletRequest request, HttpServletResponse response){
		String id = request.getParameter("id");
		boolean flag=true;
		try {
			flag = service.idCheck(id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("JsonView","result", flag);
	}
	public ModelAndView update(HttpServletRequest request, HttpServletResponse response,MemberVO vo){
		HttpSession session = request.getSession(false);
		MemberVO vo2 = (MemberVO) session.getAttribute("mvo");
		MultipartFile mfile = vo.getUploadImg();
		vo.setProImg(vo2.getProImg());
		try {
			service.update(vo,path);
			MemberVO mvo = service.login(vo.getId(), vo.getPassword());
			session.setAttribute("mvo", mvo);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("main.member");
	}
	public ModelAndView leave(HttpServletRequest request, HttpServletResponse response, HttpSession session){
		MemberVO vo = (MemberVO) session.getAttribute("mvo");
		session.invalidate();
		try {
			service.leave(vo.getuId(), vo.getPassword());
			File file = new File(path+vo.getProImg());
			file.delete();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("home");
	}
}
